/*
Basic Commands
client.state = [0,..]
	0 = No logueado
	1 = Logueado
	2 = Conectado a un chat
*/
exports.iniExt = function(main){
	main.addExt({
		ini: function(){
			//this.rooms = {1:[]}; //room[chat][nacho.igcogi][2]; //room[chat][chat][n]
			this.addNewCommand({
				r: 'login'
				,fun: this.login
			});
			this.addNewCommand({
				r: 'loginA'
				,fun: this.loginA
			});
			this.addNewCommand({
				r: 'joinChat'
				,fun: this.joinChat
			});
			this.addNewCommand({
				r: 'makePrivate'
				,fun: this.makePrivate
			});
			this.addNewCommand({
				r: 'listUsers'
				,fun: this.listUsers
				,answerFun: Function.from()
			});
			this.addNewCommand({
				r: 'newMsg'
				,fun: this.newMsg
				,answerFun: Function.from()
			});
		}
		/* command.args: username, password */
		,login: function(client, command){
			if(client.state != 0) return this.makeAnswer(command, {ok: false});
			if(!command.username || !command.password) return this.makeAnswer(command, {ok: "fatal"});
			/* Comprobación del léxico del username, y el password */
				/* Incrrecto */
				return this.makeAnswer(command, {ok: false});
			/* Comprobar que los datos del usuario existan en la base de datos */
				/* Existe */
				/* Realizar conexión de usuario en BD */
				client.state = 1;
				client.idnick = client.id /* Identificador del nick del usuario en la BD */
				return this.makeAnswer(command, {ok: true});
				/* No existe */
				return this.makeAnswer(command, {ok: false});			
		}
		/* command.args: */
		,loginA: function(client, command){
			if(client.state != 0) return this.makeAnswer(command, {ok: false});
			/* Generar nick aleatorio y guardarlo en la base de datos */
			//Conectar Anonimo
			anonNick = this.mysql.conectNickA(client.idchat);
			//var anonNick = Math.floor(Math.random()*500);
			client.state = 1;
			return this.makeAnswer(command, {ok: true, nick: anonNick});
		}
		/* command.args: idchat */
		,joinChat: function(client, command){
			if(client.state != 1) return this.makeAnswer(command, {ok: false});
			if(!command.args.idchat) return this.makeAnswer(command, {ok: "fatal"});
			
			var idchat = command.args.idchat;
			if(!this.chat.exists(idchat)) return this.makeAnswer(command, {ok: false});
			
			/* Conectar usuario al idchat en BD*/
			this.chats[idchat].connectClient(client);
			client.idchat = idchat;
			client.state = 2;
			return this.makeAnswer(command, {ok: true});
		}
		/* command.args: idclient */
		,makePrivate: function(client, command){
			if(client.state != 2) return this.makeAnswer(command, {ok: false});
			
			if(!command.args.idclient) return this.makeAnswer(command, {ok: "fatal"});
			
			if(!this.clients[idclient]) return this.makeAnswer(command, {ok: false});
			var idpriv = this.chats[client.idchat].makePrivateWith(client, this.clients[command.clients[idclient]] );
			
			if(idpriv) return this.makeAnswer(command, {ok: true, priv: idpriv});
			else return this.makeAnswer(command, {ok: false});
		}
		/* command.args: */
		,listUsers: function(client, command){
			if(client.state != 2) return this.makeAnswer(command, {ok: false});
			
			var idchat = client.idchat;
			return this.makeAnswer(command, {ok: true, users: this.rooms[idchat][idchat]});
		}
		/* command.args: priv, msg */
		//command.args.priv :- 0 significa que command.args.msg va a la sala pública.
		//					:- >0 significa que command.args.msg va a la sala "priv" (sala privada).
		,newMsg: function(client, command){
			if(client.state != 2) return this.makeAnswer(command, {ok: false});
			if(!command.args.msg || (!command.args.priv && command.args.priv !== 0)) return this.makeAnswer(command, {ok: "fatal"});
			
			var msg = command.args.msg, priv = command.args.priv;
			var room = this.chats[client.idchat].newMessage(client, msg, priv);
			if(!room) return this.makeAnswer(command, {ok: fatal});

			var multiAnswer = [];
			Object.each(room, function(c){
				if(c.id == client.id) return;
				c = this.clients[c.id];

				var a = {'priv': priv, 'msg': msg};
				var mc = this.makeCommand(c, "newMsg", a);
				multiAnswer.push( mc );
			}, this);
			
			return this.makeAnswerForAll(command, {ok: true}, multiAnswer, priv);
		}
		,chat: {
			exists: function(idchat){
				if(!this.chats[idchat])
					var c = require('../chatDef.js').loadChat(idchat);
				else var c = this.chats[idchat];
				if(c) {
					this.chats[idchat] = c;
					return true;
				} else
					return false;
			}.bind(this)
			
		}
	});
}
